/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo.Controler;

import Model.Beans.Book;
import Model.Beans.Tag;
import Model.Servicios.BookSrv;
import Model.Utilities.Constantes.BookState;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Manuel Lap
 */
public class ShareBookAction extends ActionSupport{
    
    private Book book = new Book();
    private BookSrv srvBook = new BookSrv();
    private String tags ;
    private String uploadContentType;// The content type of the file

    private String uploadFileName;// The uploaded file name and path

    private String fileCaption;
    
    private String filename;
    
    private File upload;
    
    public String compartir(){
        try {
            String name = upload.getName();
            String path = upload.getPath();
            String fullFileName = "/images";

            File theFile = new File(fullFileName);

            FileUtils.copyFile(upload, theFile);

            } catch (Exception e) {

                addActionError(e.getMessage());
            }
        String tagsList[] = tags.split(" ");
        Set lista = (Set) new HashSet<Tag>(convertToArray(tagsList));
        book.setTags(lista);
        book.setStatus(BookState.PENDIENTE);
        srvBook.addBookTags(book);
        return "";
    }
    
    
    private ArrayList<Tag> convertToArray(String tags[]){
        ArrayList<Tag> lista = new ArrayList<Tag>();
        for(int i = 0; i < tags.length; i++){
            Tag tag = new Tag();
            tag.setTag(tags[i]);
            lista.add(tag);
        }
        return lista;
    }
    
    @Override
    public void validate(){
        if (getBook() != null) {
            if (!(getBook().getName().compareTo("") != 0)) {
                addFieldError("book.name", "Faltan nombre");
            }
            if (!(getBook().getAuthor().compareTo("") != 0)) {
                addFieldError("book.author", "Falta autor");
            }
            if (!(getBook().getEditorial().compareTo("") != 0)) {
                addFieldError("book.editorial", "Falta editorial");
            }
            if (!(getBook().getIsbn().compareTo("") != 0)) {
                addFieldError("studentBean.user.password", "Falta isbn");
            }
            if (!(getBook().getYear() != 0)) {
                addFieldError("book.year", "Falta año");
            }
        }
    }

    /**
     * @return the book
     */
    public Book getBook() {
        return book;
    }

    /**
     * @param book the book to set
     */
    public void setBook(Book book) {
        this.book = book;
    }

    /**
     * @return the tags
     */
    public String getTags() {
        return tags;
    }

    /**
     * @param tags the tags to set
     */
    public void setTags(String tags) {
        this.tags = tags;
    }

    /**
     * @return the uploadContentType
     */
    public String getUploadContentType() {
        return uploadContentType;
    }

    /**
     * @param uploadContentType the uploadContentType to set
     */
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    /**
     * @return the uploadFileName
     */
    public String getUploadFileName() {
        return uploadFileName;
    }

    /**
     * @param uploadFileName the uploadFileName to set
     */
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    /**
     * @return the fileCaption
     */
    public String getFileCaption() {
        return fileCaption;
    }

    /**
     * @param fileCaption the fileCaption to set
     */
    public void setFileCaption(String fileCaption) {
        this.fileCaption = fileCaption;
    }

    /**
     * @return the upload
     */
    public File getUpload() {
        return upload;
    }

    /**
     * @param upload the upload to set
     */
    public void setUpload(File upload) {
        this.upload = upload;
    }

    /**
     * @return the filename
     */
    public String getFilename() {
        return filename;
    }

    /**
     * @param filename the filename to set
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }
}
